--
-- Author: shunguo.chen
-- Date: 2016-06-20 12:47:35
--
-- FriendView

local TabsBtn = require("app.widget.TabsBtn")
local TabsScrollBtn = require("app.widget.TabsScrollBtn")
local FriendRender = import(".renders.FriendRender")
local FriendRenderForMsdk = import(".renders.FriendRenderForMsdk")
local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.FriendScene

local SEARCH_CD = 3 -- 搜索具体目标CD时间
local FRIEND_MAX_NUM = tonumber(GD:getSystemPar("maxfriends",50))
local BLACK_MAX_NUM = tonumber(GD:getSystemPar("maxblacklist",50))

local FRIEND_STR = L("friend")
local SEARCH_STR = L("lua_code_text_486")
local APPLY_STR = L("lua_code_text_144")
local BLACK_STR = L("lua_code_text_351")
local FRIEND_NUM_STR = L("lua_code_text_424")
local BLACK_NUM_STR = L("lua_code_text_129")

local FRIEND_IDX = 1
local ADD_IDX = 2
local APPLY_IDX = 3
local BLACK_IDX = 4
local MSDK_IDX = 5

local FRIEND_NO_STR = L("lua_code_text_371")
local ADD_NO_STR = L("lua_code_text_344")
local SEARCH_NO_STR = L("lua_code_text_368")

function ClassRef:init()
	self._ui.Button_close:addClickEventListener(function()
			executeFunc(self._onCloseView)
		end)

	self._titleText = self._ui["Panel/Panel_tlt/tlt"]
	self._numText = self._ui["Panel/Panel_fri/Text_num"]

	-- list
	self._friendList = self._ui.ListView
	self._initSize = self._friendList:getContentSize()
	self._initPosY = self._friendList:getPositionY()
	self._friendList:removeAllItems()

	self._friendList.createRender = function (list, idx)
		local items = list:getItems()
    	local render = items[idx]
		if nil == render then
			if self._curIdx == MSDK_IDX then
				render = FriendRenderForMsdk.create()
			else
				render = FriendRender.create()
			end
			list:pushBackCustomItem(render)
		end
		render:setVisible(true)
		return render
	end

	local panel = self._ui["Panel/Panel"]
	self._addHeight = panel:getContentSize().height
	self._addPosY = panel:getPositionY()

	-- search
	self:initSearch()

	-- tabs
	local btns = {0,0,0}

	local maxTabIndex = 4
	if UD.msdkData:isQQPlatform() then
		self._msdkTabName = L("lua_code_text_174")
		self._ui["Panel_tab/selected_btn_5/name_1"]:setString(self._msdkTabName)
		self._ui["Panel_tab/selected_btn_5/name_2"]:setString(self._msdkTabName)
		maxTabIndex = 5
	elseif UD.msdkData:isWXPlatform() then
		maxTabIndex = 5
		self._msdkTabName = L("lua_code_text_215")
		self._ui["Panel_tab/selected_btn_5/name_1"]:setString(self._msdkTabName)
		self._ui["Panel_tab/selected_btn_5/name_2"]:setString(self._msdkTabName)
	else
		maxTabIndex = 4
		self._ui["Panel_tab/selected_btn_5"]:setVisible(false)
	end

	for i=1, maxTabIndex do
		btns[i] = self._ui.Panel_tab:getChildByName("selected_btn_" .. i)
	end

    local function onTabsBtn(index)
    	if self._curIdx then
    		self._curIdx = index
	    	self:switchPanel()
	    else
	    	self._curIdx = index
	    end
    end
	self._tabsBtn = TabsBtn.new(btns, onTabsBtn, 1)
	
	self._ui["Panel/Panel_fri/btn_add"]:addClickEventListener(function()
		UD.msdkData:inviteFriend()
	end)
end

function ClassRef:show(index)
	self:setVisible(true)
	self:switchPanel(index)
	self:openAction()
end

function ClassRef:onSwitchPanel(cb)
	self._onSwitchPanel = cb                  
end

function ClassRef:getFriendList()
	return self._friendList
end

function ClassRef:initSearch()

	local function onEditCallback(strEventName,sender)
        local edit = sender 
		if strEventName == "began" then
        elseif strEventName == "ended" then
        	
        elseif strEventName == "return" then -- 之前输入完成就请求？

        elseif strEventName == "changed" then
            
        end
	end

	self._searchPanel = self._ui.Panel_add:getChildByName("Panel_search")
	self._searchHeight = self._searchPanel:getContentSize().height

	local tf = self._searchPanel:getChildByName("TextField")
	self._editText = display.replaceTextFieldWithEditBox(tf)
	self._editText:registerScriptEditBoxHandler(onEditCallback)

	local btn = self._searchPanel:getChildByName("Button_enter")
	btn:addClickEventListener(function(sender)
			local str = self._editText:getText()
            if string.len(str) > 0 then
            	self._editText:setText("")
				executeFunc(self._onOperate, "search", str)
				-- protect 3s
				display.setBtnTouchEnable(sender, false)
				sender:performWithDelay(function()
						display.setBtnTouchEnable(sender, true)
					end, SEARCH_CD)
			end
		end)


	local function onTabsBtn(index)
		if self._searchIdx then
    		self._searchIdx = index
			-- executeFunc(self._onOperate, "getSearchs", index)
			self:refreshList()
	    else
	    	self._searchIdx = index
	    end
	end
	local btnPanel = self._ui.Panel_add:getChildByName("Panel_btn")
	self._tabsSBtn = TabsScrollBtn.new(btnPanel, onTabsBtn, 2)
end

function ClassRef:switchPanel(idx)
	if idx then
		self._curIdx = idx
		self._tabsBtn:setSelected(idx)
	end

	local title
	if self._curIdx == FRIEND_IDX then
		title = FRIEND_STR
	elseif self._curIdx == ADD_IDX then
		title = SEARCH_STR
	elseif self._curIdx == APPLY_IDX then
		title = APPLY_STR
	elseif self._curIdx == BLACK_IDX then
		title = BLACK_STR
	elseif self._curIdx == MSDK_IDX then
		title = self._msdkTabName
	end
	self._titleText:setString(title)
	
	self._ui.Panel_fri:setVisible(self._curIdx == FRIEND_IDX or self._curIdx == BLACK_IDX or self._curIdx == MSDK_IDX)
	self._ui.Panel_add:setVisible(self._curIdx == ADD_IDX)
	self._ui.Panel_apply:setVisible(self._curIdx == APPLY_IDX)

	self:refreshList()
end

function ClassRef:refreshList()
	local posY
	local height
	if self._curIdx == ADD_IDX then
		posY = self._addPosY
		self._searchPanel:setVisible(self._searchIdx == 4)
		if self._searchIdx == 4 then
			height = self._addHeight
		else
			height = self._addHeight + self._searchHeight
		end
	elseif self._curIdx == APPLY_IDX then
		posY = self._addPosY
		height = self._ui.Panel_apply:getContentSize().height - self._ui.Panel_apply:getChildByName("bg1"):getContentSize().height
	else
		posY = self._initPosY
		height = self._initSize.height
	end

	self._friendList:setPositionY(posY)
	self._friendList:setContentSize(cc.size(self._initSize.width, height))

	executeFunc(self._onSwitchPanel, self._curIdx, self._searchIdx)
end

function ClassRef:refreshNum(num)
	local str
	self._ui["Panel/Panel_fri/btn_add"]:setVisible(false)
	if self._curIdx == FRIEND_IDX then
		str = string.format(FRIEND_NUM_STR, num, FRIEND_MAX_NUM)
	elseif self._curIdx == BLACK_IDX then
		str = string.format(BLACK_NUM_STR, num, BLACK_MAX_NUM)
	elseif self._curIdx == MSDK_IDX then
		str = string.format("%s：%s/%s", self._msdkTabName,num, BLACK_MAX_NUM)
		self._ui["Panel/Panel_fri/btn_add"]:setVisible(true)
	else
		str = num
	end
	self._numText:setString(str)
	local posx = self._numText:getPositionX()
	local size = self._numText:getVirtualRenderer():getContentSize()
	self._ui["Panel/Panel_fri/btn_add"]:setPositionX(posx + size.width/2 + 20)

	self._ui.Panel_tips:setVisible(num == 0)
	if num == 0 then
		local tipStr
		if self._curIdx == FRIEND_IDX then
			tipStr = FRIEND_NO_STR
		elseif self._curIdx == ADD_IDX then
			tipStr = ADD_NO_STR
		elseif self._curIdx == MSDK_IDX then
			tipStr = FRIEND_NO_STR
		else
			tipStr = SEARCH_NO_STR
		end
		self._ui.Panel_tips:getChildByName("text"):setString(tipStr)
	end
end

function ClassRef:cleanListView()
	-- if self._curIdx == MSDK_IDX and self._friendList then
		self._friendList:removeAllItems()
	-- end
end

-- 是否有新的好友
function ClassRef:setNewTipVisibla(b)

end

function ClassRef:remove()
	
end

return ClassRef